package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.basicdata.dao.BasePropMapper;
import com.redoor.app.stake.basicdata.model.BaseProp;
import com.redoor.app.stake.basicdata.model.constants.BasePropConstants;
import com.redoor.app.stake.basicdata.model.constants.BasicDataRedisConstants;
import com.redoor.app.stake.basicdata.model.controllerModel.BasePropCriteria;
import com.redoor.app.stake.basicdata.service.BasePropRedisService;
import com.redoor.app.stake.basicdata.service.BasePropService;
import com.redoornetwork.framework.data.redis.util.RedisUtils;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by wangjunlei on 2018-03-10 17:05:59.
 */
@Service
public class BasePropServiceImpl extends AbstractService<BaseProp, String> implements BasePropService {
    private Logger logger = LoggerFactory.getLogger(BasePropServiceImpl.class);
    @Autowired
    private BasePropMapper basePropMapper;

    @Autowired
    private BasePropRedisService basePropRedisService;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public void initPropToRedis(boolean isDistraint) {
        if(isDistraint){
            logger.info("remove prop key...");
            redisUtils.removePattern(BasicDataRedisConstants.REDIS_PROP_PRE);
        }
        List<BaseProp> allList = this.findAll();
        if(allList == null || allList.size() == 0){
            logger.warn("prop list is empty...");
            return;
        }
        basePropRedisService.insertToRedis(allList);
    }

    @Override
    public BaseProp getProp(BasePropCriteria criteria) {
        List<BaseProp> props = basePropMapper.queryNames(criteria);
        BaseProp prop = (props!=null && props.size()>0)?props.get(0):null;
        return prop;
    }

    @Override
    public String getProp(String name) {
        if (StringUtils.isEmpty(name)){
            return "";
        }
        //判断数据库中是否存在，不存在则差表
        if(!redisUtils.hHasKey(BasicDataRedisConstants.REDIS_PROP_PRE,name.toUpperCase())){
            BasePropCriteria propCriteria = new BasePropCriteria();
            propCriteria.setName(name);
            BaseProp prop =  this.getProp(propCriteria);
            if(null != prop){
                redisUtils.hset(BasicDataRedisConstants.REDIS_PROP_PRE,name.toUpperCase(), prop.getValue(),BasicDataRedisConstants.REDIS_OVER_TIME_DAY);
            }
        }
        return basePropRedisService.getPropFromRedis(name);
    }

    @Override
    public double getRegisterIndex() {
        //不存在则查询数据库
        if(!redisUtils.hHasKey(BasicDataRedisConstants.REDIS_PROP_PRE,BasePropConstants.registrationIndex.toUpperCase())){
            getProp(BasePropConstants.registrationIndex);
        }
        //递增1返回
        return redisUtils.hincr(BasicDataRedisConstants.REDIS_PROP_PRE, BasePropConstants.registrationIndex,1);
    }

    @Override
    public boolean isLocal() {
        return new Boolean(getProp(BasePropConstants.isLocal));
    }

    @Override
    public boolean initMQFlag() {
        return new Boolean(getProp(BasePropConstants.mqFlag));
    }
}